/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pt.moltos.rabbit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Message sender controller.
 * @author MoLt1eS
 */
public class Send {
    

    public Send() {
    }
    
    /**
     * Sends message to rabbitMQ server
     * @param message As the message to send
     * @return boolean as the final state of the called function
     */
    public boolean sendMessage(String message){
        try {
            ConnectionFactory factory = new ConnectionFactory();
            
            factory.setHost(RabbitController.SERVER_IP);
            factory.setUsername(RabbitController.USERNAME);
            factory.setPassword(RabbitController.PASSWORD);
            
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            
            channel.exchangeDeclare(RabbitController.EXCHANGE_NAME, "fanout");
            
            channel.basicPublish(RabbitController.EXCHANGE_NAME, "", null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
            
            channel.close();
            connection.close();
            
            return true;
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    
}
